package com.example.demo.controller;

import com.example.demo.bean.Goods;
import com.example.demo.bean.Goodsinfo;
import com.example.demo.bean.HttpResult;
import com.example.demo.bean.User;
import com.example.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
   @Autowired
    GoodsService goodsService;
    @RequestMapping("/queryList")
    public HttpResult<List<Goods>> queryList(@RequestBody Goods goods){
        HttpResult<List<Goods>> result = new HttpResult<>();
        List<Goods> list = goodsService.queryGoodList(goods);
        result.setData(list);
        return result;
    }

    @RequestMapping("/queryGoodsInfo")
    public HttpResult<Goods> queryGoodsInfo(int id){
        HttpResult<Goods> result = new HttpResult<>();
        Goods goods = goodsService.queryGood(id);
        if(goods != null){
            result.setData(goods);
        }
        return result;
    }
    @RequestMapping("/addGoods")
    public HttpResult<Integer> addGoods(@RequestBody Goods goods, HttpServletRequest request) {
        HttpResult<Integer> httpResult = new HttpResult<>();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        goods.setUserid(user.getId());
        goods = goodsService.addGoods(goods);
        httpResult.setData(goods.getId());
        return httpResult;
    }
    @RequestMapping("/addimg")
    public HttpResult<Boolean> addimg(MultipartFile file, int id) throws IOException {
        Goods goods = goodsService.queryGood(id);
        HttpResult<Boolean> result = new HttpResult<>();
        if(file.isEmpty()){
            result.setMsg("文件無效");
            result.setData(false);
        }else {
            String filename = file.getOriginalFilename();
            filename = System.currentTimeMillis()+filename;
            file.transferTo(new File("D:\\upload/"+filename));
            goods.setSrc("/"+filename);
            goodsService.updateGoods(goods);
            result.setMsg("上傳成功");
            result.setData(true);
        }
        return result;
    }
    @RequestMapping("/deleteGoods")
    public HttpResult<Boolean> deleteGoods(@RequestBody Goods goods){
        HttpResult<Boolean> result = new HttpResult<>();
        String src=goods.getSrc();
        int i = goodsService.deleteGoods(goods.getId());
        new File("D:\\upload/" + src).delete();
        if(i==1){
            result.setData(true);
        }
        else {
            result.setData(false);
        }
        return result;
    }

    @RequestMapping("/update")
    public HttpResult<Boolean> update(@RequestBody Goods goods){
        HttpResult<Boolean> result = new HttpResult<>();
        int i = goodsService.update(goods);
        if(i==1){
            result.setData(true);
        }
        else {
            result.setData(false);
        }
        return result;
    }
}
